package com.hwh.springdatajpademo.controller;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestDemoController{

    public static void main(String[] args) {
        FunctionTest();
    }

    /**
     * 创建流
     */
    public static void testCreateStream(){
        List<String> list = new ArrayList<>();
        Stream<String> stream = list.stream();//获取一个顺序流
        Stream<String> stringStream = list.parallelStream();//获取一个并行流
    }

    /**
     * 数组转换成流
     */
    public static void testNumsCheckoutToStream(){
        Integer[] nums = new Integer[]{1,2,34,5,6};
        Stream<Integer> stream = Arrays.stream(nums);
        stream.forEach(System.out::println);
        //流转换成List
        List<Integer> collect = stream.collect(Collectors.toList());
        Set<Integer> collect1 = stream.collect(Collectors.toSet());
        List<Integer> collect2 = collect1.stream().collect(Collectors.toList());
    }
    /**
     * 测试Map和FlatMap
     */
    public static void testMapAndFlatMap(){
        Integer[] nums = new Integer[]{1,2,34,5,6};
        Stream<Integer> stream = Arrays.stream(nums);
        stream.map(n -> n * n).collect(Collectors.toList()).forEach(System.out::println);

        List<String[]> eggs = new ArrayList<>();
        eggs.add(new String[]{"煎蛋1","煎蛋2","煎蛋3","煎蛋4","煎蛋5"});
        eggs.add(new String[]{"煎蛋1","煎蛋2","煎蛋3","煎蛋4","煎蛋5"});

        eggs.stream().map(x -> Arrays.stream(x).map(str -> str.replace('煎','鸡'))).forEach(arr-> System.out.println(Arrays.toString(arr.toArray())));
        Stream<List<Integer>> inputStream = Stream.of(
                Arrays.asList(1),
                Arrays.asList(2, 3),
                Arrays.asList(4, 5, 6)
        );
        Stream<Integer> outputStream = inputStream.
                flatMap(Collection::stream);
    }

    /**
     * 测试Filter过滤
     */
    public static void testFilter(){
        Integer[] nums = new Integer[]{1,2,34,5,6};
        Stream<Integer> stream = Arrays.stream(nums);
        stream.filter(x-> x % 2 == 0).forEach(System.out::println);
    }
    /**
     * 测试Foreach
     */
    public static void testForeach(){
        Stream.of(1,2,77,88,321,845,12,3124,3,64).forEach(x -> {
            System.out.println(x*x);
        });
    }
    /**
     * 测试FindFirst
     */
    public static void testFindFirst(){
        System.out.println(Stream.of(1, 2, 77, 88, 321, 845, 12, 3124, 3, 64).findFirst());
    }
    /**
     * 测试Optional
     */
    public static void testOptional(){
        Stream<Integer> integerStream = Stream.of(1, 2, 77, 88, 321, 845, 12, 3124, 3, 64);
        Optional.of(integerStream).ifPresent(x -> x.forEach(System.out::println));
    }
    public static void testReduce(){
        //acc:第一个元素
        //item:第二个元素，第二次遍历时值会是第一次遍历的结果
        //return：在这个逻辑里是返回第一个元素和第二个元素的和，并用于下次遍历的item上
        Optional accResult = Stream.of(1, 2, 5, 4).reduce((acc, item) -> {
            System.out.println("acc : " + acc);
            acc += item;
            System.out.println("item: " + item);
            System.out.println("acc+ : " + acc);
            System.out.println("--------");
            return acc;
        });
        System.out.println(accResult);
        //字符串拼接
        String appendRes = Stream.of("a","p","p","l","e")
                .reduce("", String::concat);
        System.out.println(appendRes);
    }
    public static void testSkipAndLimit(){
        Stream.of(1,2,3,4,5).limit(2).forEach(System.out::println);
        System.out.println("_________________________");
        Stream.of(1,421,6,6,37,53,4,235,5637,43,221,3,4,5).sorted(Integer::compareTo).skip(2).forEach(System.out::println);
    }
    public static void testMaxAndMin(){
        System.out.println(Stream.of(1, 2, 3, 4, 5).max(Integer::compareTo));
        System.out.println("_________________________");
        System.out.println(Stream.of(1, 421, 6, 6, 37, 53, 4, 235, 5637, 43, 221, 3, 4, 5).min(Integer::compareTo));
    }
    public static void testDistinct(){
        Stream.of(1,421,6,6,37,53,4,235,5637,43,221,3,4,5).distinct().sorted(Integer::compareTo).forEach(System.out::println);
    }
    public static void testConsumer(){
        Consumer f = System.out::println;
        Consumer f2 = o -> System.out.println(o+"f2");
        //执行完F后再执行F2的Accept方法
        f.andThen(f2).accept("test");

        //连续执行F的Accept方法
        f.andThen(f).andThen(f).andThen(f).accept("test1");
    }
    public static void FunctionTest(){
        Function<Integer,String> func = element -> String.valueOf(element+"String");

        System.out.println(func.apply(15));
    }
}
